package com.shop.loan.aop;

import com.alibaba.fastjson.JSON;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.Constant;
import com.shop.loan.model.common.RequestBase;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.User;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
@Aspect
@Order(2)
public class LoginAuthAspect {

    @Resource
    RedisUtil redisUtil;

    @Pointcut("@annotation(com.shop.loan.annotation.LoginAuth)")
    private void login() {

    }

    @Around("login()")
    public Object LoginAuth(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpServletRequest = attributes.getRequest();

        String passUrl = "/home/get";
        String passUrl2 = "/goods/list";

        log.info("需要校验登录");
        String authToken = httpServletRequest.getHeader(Constant.TOKEN);
        if (!StringUtils.hasLength(authToken) && !httpServletRequest.getServletPath().contains(passUrl)
                && !httpServletRequest.getServletPath().contains(passUrl2)) {
            return new ResResult(Code.NEED_LOGIN);
        }
        String tokenValue = redisUtil.get(RedisCache.rentalUserLogin + authToken);
        if (!StringUtils.hasLength(tokenValue) && !httpServletRequest.getServletPath().contains(passUrl)
                && !httpServletRequest.getServletPath().contains(passUrl2)) {
            return new ResResult(Code.NEED_LOGIN);
        }
        if(!StringUtils.hasLength(tokenValue) && (httpServletRequest.getServletPath().contains(passUrl)
                || httpServletRequest.getServletPath().contains(passUrl2))){
            return joinPoint.proceed();
        }
        User rentalUser = JSON.parseObject(tokenValue, User.class);
        redisUtil.expire(RedisCache.rentalUserLogin + authToken, Constant.TOKEN_TIME_OUT.intValue());

        Object[] obj = joinPoint.getArgs();
        for (Object argItem : obj) {
            if (argItem != null && argItem instanceof RequestBase) {
                RequestBase requestBase = (RequestBase) argItem;
                /**
                 * 将常用参数存储到请求基类中
                 */
                requestBase.setOpId(rentalUser.getId());
                requestBase.setAuthToken(authToken);
                requestBase.setOpIp(IpUtil.getIpAddr(httpServletRequest));
            }
        }
        return joinPoint.proceed();
    }

}
